<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>这里完成对选课学生对显示</h1>
<table>
    <tr>
        <th>课程号</th>
        <th>课程名称</th>
        <th>学期</th>
        <th>学生学号</th>
        <th>学生姓名</th>
        <th>成绩</th>
        <th>修改</th>
    </tr>
    <c:forEach items="${s_grade_inf}" var="s_grade">
        <tr>
            <td>${s_grade.course_id}</td>
            <td>${s_grade.course_name}</td>
            <td>${s_grade.term}</td>
            <td>${s_grade.id}</td>
            <td>${s_grade.student_name}</td>
            <td>
                <input type="text" id="grade_${s_grade.id}" value="${s_grade.grade}" />
            </td>
            <td>
                <button onclick="updateGrade('${s_grade.id}', '${s_grade.course_id}','${s_grade.teacher_id}',)">Update</button>
            </td>
        </tr>
    </c:forEach>
</table>

<script>

        function updateGrade(id, courseId,tid) {
        // Get the updated grade value from the input field

        const gradeInput = document.getElementById(`grade_`+id);
        const newGrade = gradeInput.value;
        console.log(id,courseId,tid);
        // Create a hidden form and append input fields
        const form = document.createElement('form');
        form.method = 'POST';
        form.action = '/TeacherCourseDetail'; // Replace with the actual URL of your servlet

        const idInput = document.createElement('input');
        idInput.type = 'hidden';
        idInput.name = 'sid';
        idInput.value = id;//学号
        form.appendChild(idInput);

            const tidInput = document.createElement('input');
            tidInput.type = 'hidden';
            tidInput.name = 'tid';
            tidInput.value = tid;//学号
            form.appendChild(tidInput);

        const courseIdInput = document.createElement('input');
        courseIdInput.type = 'hidden';
        courseIdInput.name = 'courseId';
        courseIdInput.value = courseId;
        form.appendChild(courseIdInput);

        const gradeInputField = document.createElement('input');
        gradeInputField.type = 'hidden';
        gradeInputField.name = 'newGrade';
        gradeInputField.value = newGrade;
        form.appendChild(gradeInputField);

        document.body.appendChild(form);
        //     $.ajax({
        //         url: '/TeacherCourseDetail', // Replace with the actual URL of your servlet
        //         type: 'POST',
        //         data: {
        //             id: id,
        //             courseId: courseId,
        //             tid:tid,
        //             newGrade: newGrade
        //         },
        //         success: function(response) {
        //             // Handle the response here
        //             console.log('Response:', response);
        //             // You can update the page or perform other actions based on the response
        //         },
        //         error: function(xhr, status, error) {
        //             console.error('Error:', error);
        //         }
        //     });

         form.submit();
    }
</script>
</body>
</html>